<?php

namespace app\admin\controller;
use app\admin\service\OssService;
use app\admin\model\PohtoUser as PohtoUserModel;
use app\admin\validate\CommonnValidate;
use app\admin\validate\PhotoValidate;
use app\BaseController;

class PhotoUser extends BaseController
{
    protected $middleware = ['Auth'];
    public function list() {
        $params = $this->request->post(['current', 'size', 'username', 'country', 'institution_id', 'id']);
        $validate = new CommonnValidate();
        if (!$validate->scene('list')->check($params)) {
            return result()->fail($validate->getError(), 500);
        }
        $sql = new PohtoUserModel();
        $whereArr = [];
        if(isset($params['username']) && $params['username']) {
            $whereArr[] = ["username", "LIKE", '%'.$params['username'].'%'];
        }
        if(isset($params['id']) && $params['id']) {
            $whereArr[] = ["id", "=", $params['id']];
        }
        if(isset($params['institution_id']) && $params['institution_id']) {
            $whereArr[] = ["institution_id", "=", $params['institution_id']];
        }
        if(isset($params['country']) && $params['country']) {
            $whereArr[] = ["country", "=", $params['country']];
        }
        $list = $sql
            ->where($whereArr)
            ->with('institutionName')
            ->order('sort desc')
            ->order('create_at desc')
            ->paginate([
                'list_rows'=> request()->param('size'),
                'page' => request()->param('current'),
            ]);
        if(config('app.imgtype') == 'other') {
            foreach ($list as $k => $item) {
                $item['avatar_show'] = $item['avatar'];
            }
            return result()->success($list);

        } else {
            $oss = new OssService();
            return result()->success($oss->signIds($list, 'avatar_id', 'avatar_show'));

        }
    }

    public function addEdit() {
        $params = $this->request->post(['id']);
        if(isset($params['id']) && $params['id']) {
            return $this->edit();
        }
        return $this->add();
    }

    public function add() {
        $params = $this->request->post(['username', 'institution_id', 'avatar', 'avatar_id', 'country', 'sort']);
        $validate = new PhotoValidate();
        if (!$validate->scene('addUser')->check($params)) {
            return result()->fail($validate->getError(), 500);
        }
        $sql = new PohtoUserModel();
        $sql->save($params);
        return result()->success("添加成功");
    }

    public function edit() {
        $params = $this->request->post(['id', 'username', 'hot', 'institution_id', 'avatar', 'avatar_id', 'country', 'sort']);
        $validate = new CommonnValidate();
        if (!$validate->scene('edit')->check($params)) {
            return result()->fail($validate->getError(), 500);
        }
        $sql = new PohtoUserModel();
        $info = $sql->find($params['id']);
        $info->save($params);
        return result()->success("修改成功");
    }

    public function dels() {
        $params = $this->request->post(['ids']);
        $validate = new CommonnValidate();
        if (!$validate->scene('dels')->check($params)) {
            return result()->fail($validate->getError(), 500);
        }
        $sql = new PohtoUserModel();
        $sql->destroy($params['ids']);
        return result()->success("删除成功");
    }
}